package cc.magicjson.tools.chain.domain;

/**
 * 订单实体类
 */
public record Order(boolean isVIP,            // 是否VIP用户
                    boolean hasDiscount,      // 是否有优惠
                    double amount,            // 订单金额
                    boolean isPaid,           // 是否已支付
                    double pointsUsed,        // 使用的积分数量
                    double couponsUsed,       // 使用的优惠券数量
                    double discountAmount,    // 总优惠金额
                    double finalAmount        // 最终支付金额
) {
    // 构造函数重载，用于简单场景
    public Order(boolean isVIP, boolean hasDiscount, double amount, boolean isPaid) {
        this(isVIP, hasDiscount, amount, isPaid, 0, 0, 0, amount);
    }

    // 构造只有基本信息的订单，用于初始化
    public static Order createInitial(double amount, boolean isVIP) {
        return new Order(isVIP, false, amount, false, 0, 0, 0, amount);
    }

    // 创建一个使用积分后的新订单
    public Order withPoints(double points, double discountValue) {
        double newDiscount = this.discountAmount + discountValue;
        double newFinal = this.amount - newDiscount;
        return new Order(this.isVIP, true, this.amount, this.isPaid, points, this.couponsUsed, newDiscount, newFinal);
    }

    // 创建一个使用优惠券后的新订单
    public Order withCoupons(double coupons, double discountValue) {
        double newDiscount = this.discountAmount + discountValue;
        double newFinal = this.amount - newDiscount;
        return new Order(this.isVIP, true, this.amount, this.isPaid, this.pointsUsed, coupons, newDiscount, newFinal);
    }

    // 创建一个已支付的订单
    public Order paid() {
        return new Order(this.isVIP, this.hasDiscount, this.amount, true, this.pointsUsed, this.couponsUsed, this.discountAmount, this.finalAmount);
    }

    // 重设优惠为零的订单（用于回滚）
    public Order resetDiscount() {
        return new Order(this.isVIP, false, this.amount, this.isPaid, 0, 0, 0, this.amount);
    }

    @Override
    public String toString() {
        return "Order{" + "isVIP=" + isVIP + ", hasDiscount=" + hasDiscount + ", amount=" + amount + ", isPaid=" + isPaid + ", pointsUsed=" + pointsUsed + ", couponsUsed=" + couponsUsed + ", discountAmount=" + discountAmount + ", finalAmount=" + finalAmount + '}';
    }
}
